//
//  YDSelectBuyBlockFooter.m
//  PeiZiWangBei
//
//  Created by 巨彦新 on 2018/1/22.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDSelectBuyBlockFooter.h"

@interface YDSelectBuyBlockFooter ()

//计算的股数和资金利用率
@property (nonatomic , weak) UILabel *stockRate;
//分割线
@property (nonatomic , weak) UIView *lineV1;
@end

@implementation YDSelectBuyBlockFooter


-(void)setStockMarket:(NSDictionary *)stockMarket
{
    _stockMarket = stockMarket;
    [self reLoadDetailsTheUtilizationFunds];
}


+ (instancetype)footerViewWithTableView:(UITableView *)tableView
{
    
    static NSString *selectBuyBlockFooter = @"YDSelectBuyBlockFooter";
    YDSelectBuyBlockFooter *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:selectBuyBlockFooter];
    if (footerView == nil) {
        footerView = [[YDSelectBuyBlockFooter alloc] initWithReuseIdentifier:selectBuyBlockFooter];
    }
    return footerView;
}


-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        [self stockRate];
        [self lineV1];
        
        
    }
    return self;
}
//购买股票的手数
-(NSInteger)calculationStockHands
{
    NSString *stockPrice = [NSString stringWithFormat:@"%@" , self.stockMarket[@"upLimitPrice"]];
    NSString *currentApplyMoney = [self.currentMoney pz_stringNumberByMultiplyingBy:@"10000"];
    NSString *numberShares = [currentApplyMoney pz_stringNumberByDividingBy:stockPrice];
    DDLog(@"numberShares = %@" , numberShares)
    NSInteger hands = [numberShares integerValue] / 100;
    return hands;
}
//计算资金利用率
-(NSString *)calculationCapitalUtilization
{
    NSString *stockPrice = [NSString stringWithFormat:@"%@" , self.stockMarket[@"upLimitPrice"]];
    NSInteger hands = [self calculationStockHands] * 100;
    NSString *useMoney = [[NSString stringWithFormat:@"%zd",hands] pz_stringNumberByMultiplyingBy:stockPrice];
    NSString *allMoney = [self.currentMoney pz_stringNumberByMultiplyingBy:@"10000"];
    float useRate = (useMoney.floatValue / allMoney.floatValue) * 100;
    NSString *perUseRate = [NSString stringWithFormat:@"%0.2f%%",useRate];
    return perUseRate;
}
-(void)reLoadDetailsTheUtilizationFunds
{
    NSString *stockName = [NSString stringWithFormat:@" %@" , self.stockMarket[@"name"]];
    NSString *handsString = [NSString stringWithFormat:@" %zd " , [self calculationStockHands]];
    NSString *fundRate = [NSString stringWithFormat:@" %@" , [self calculationCapitalUtilization]];
    NSString *string = [NSString stringWithFormat:@"可购买%@%@手，资金利用率%@",stockName , handsString , fundRate];
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange range1 = [string rangeOfString:stockName options:NSCaseInsensitiveSearch];
    NSRange range2 = [string rangeOfString:handsString options:NSCaseInsensitiveSearch];
    NSRange range3 = [string rangeOfString:fundRate options:NSCaseInsensitiveSearch];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:dark] range:range1];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:paleRed] range:range2];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:paleRed] range:range3];
    self.stockRate.attributedText = str;
}

-(UILabel *)stockRate
{
    if (_stockRate == nil) {
        UILabel *stockRate = [[UILabel alloc] init];
        [self.contentView addSubview:stockRate];
        stockRate.textColor = [UIColor colorWithHexString:blueyGrey];
        stockRate.font = [UIFont systemFontOfSize:font12ScreenWRate * LKScreenW];
        stockRate.textAlignment = NSTextAlignmentRight;
        stockRate.text = @"申请资金申请资金申请资金申请资金申请资金申请资金";
        [stockRate mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(self.contentView).offset(-rate375(16));
        
            
            if (isPhoneX) {
                make.top.equalTo(self.contentView).offset(rateX812(4));
            }else{
                make.top.equalTo(self.contentView).offset(rate750(4));
            }
        }];
        stockRate.adjustsFontSizeToFitWidth = YES;
        _stockRate = stockRate;
    }
    return _stockRate;
}

-(UIView *)lineV1
{
    if (_lineV1 == nil) {
        UIView *lineV1 = [[UIView alloc] init];
        [self.contentView addSubview:lineV1];
        lineV1.backgroundColor = NumberColor(247, 247, 247);
        [lineV1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@1);
            make.left.equalTo(self).offset(10);
            make.right.equalTo(self).offset(-10);
            if (isPhoneX) {
                make.top.equalTo(self.stockRate.mas_bottom).offset(rateX812(10));
            }else{
                make.top.equalTo(self.stockRate.mas_bottom).offset(rate750(10));
            }
        }];
        
        
        _lineV1 = lineV1;
    }
    return _lineV1;
}
@end
